package com.mapperdb.type;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;

public final class JavaType {
	/*
	 * This is added to enable basic support for the ARRAY data type - but a
	 * custom type handler is still required
	 */

	private static Map<Class<? extends Object>, JdbcType> codeLookup = new HashMap<Class<? extends Object>, JdbcType>();

	static {
		codeLookup.put(byte.class, JdbcType.TINYINT);
		codeLookup.put(int.class, JdbcType.INTEGER);
		codeLookup.put(long.class, JdbcType.NUMERIC);
		codeLookup.put(float.class, JdbcType.REAL);
		codeLookup.put(double.class, JdbcType.FLOAT);
		codeLookup.put(boolean.class, JdbcType.BIT);
		codeLookup.put(byte[].class, JdbcType.BLOB);

		codeLookup.put(Byte.class, JdbcType.TINYINT);
		codeLookup.put(Integer.class, JdbcType.INTEGER);
		codeLookup.put(Long.class, JdbcType.NUMERIC);
		codeLookup.put(Float.class, JdbcType.REAL);
		codeLookup.put(Double.class, JdbcType.FLOAT);
		codeLookup.put(Boolean.class, JdbcType.BIT);
		codeLookup.put(Byte[].class, JdbcType.BLOB);

		codeLookup.put(Date.class, JdbcType.DATE);
		codeLookup.put(java.sql.Date.class, JdbcType.DATE);
		codeLookup.put(Time.class, JdbcType.TIME);
		codeLookup.put(Timestamp.class, JdbcType.TIMESTAMP);
		codeLookup.put(BigDecimal.class, JdbcType.NUMERIC);
		codeLookup.put(Object.class, JdbcType.OTHER);
	}

	JavaType() {
	}

	public static JdbcType forCode(Class<?> code) {
		if (code == null) return null;
		return codeLookup.get(code);
	}
}
